package com.hnzq.cloud.freshorderassembly.controller;


import com.hnzq.cloud.ShopcarAPI;
import com.hnzq.cloud.dto.AddShopcarGoodsDto;
import com.hnzq.cloud.dto.PageShopcarGoodsDto;
import com.hnzq.cloud.dto.UpdateGoodsCountDto;
import com.hnzq.cloud.freshorderassembly.service.IFreShopcarService;
import com.hnzq.cloud.model.Result;
import com.hnzq.cloud.param.AddShopcarGoodsParam;
import com.hnzq.cloud.param.UpdateGoodsCountParam;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周皓月
 * @since 2022-02-06
 */
@RestController
@RequestMapping("/freShopcar")
public class FreShopcarController implements ShopcarAPI {

    @Autowired
    private IFreShopcarService fss;


    /**
     * 向购物车里添加商品信息
     * @param addShopcarGoodsParam
     * @return
     */
    @Override
    @GlobalTransactional
    public Result<AddShopcarGoodsDto> addShopcarGoods(AddShopcarGoodsParam addShopcarGoodsParam) {
        return Result.newInstance().setMessage("添加商品成功").setData(fss.addShopcarGoods(addShopcarGoodsParam));
    }

    /**
     * 查询当前用户的购物车列表
     * @return
     */
    @Override
    public Result<PageShopcarGoodsDto> findShopcarGoods() {
        return Result.newInstance().setData(fss.findShopcarGoods());
    }

    /**
     * 操作购物车，修改商品数量
     * @param updateGoodsCountParam
     * @return
     */
    @Override
    @GlobalTransactional
    public Result<UpdateGoodsCountDto> updateGoodsCount(UpdateGoodsCountParam updateGoodsCountParam) {
        return Result.newInstance().setMessage("修改成功").setData(fss.updateGoodsCount(updateGoodsCountParam));
    }

    /**
     * 操作购物车，删除商品
     * @param goodsId
     * @return
     */
    @Override
    @GlobalTransactional
    public Result<Boolean> removeGoods(Integer goodsId) {
        if (fss.removeGoods(goodsId)){
            return Result.newInstance().setMessage("删除成功");
        }
        return Result.newInstance().setCode(500).setMessage("删除失败");
    }
}

